package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.Bid;
import com.gxa.p2p.pojo.LoanBidsDetail;
import com.gxa.p2p.service.BidService;
import com.gxa.p2p.service.LoanBidsDetailService;
import com.gxa.p2p.util.MapUtil;
import com.gxa.p2p.util.ReturnMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@ResponseBody
@Api(value = "实体类LoanBidsDetail的控制器",tags = "loanBidsDetailController")
@RequestMapping("/loanBidsDetail")
public class LoanBidsDetailController {

    @Resource
    private LoanBidsDetailService loanBidsDetailService;
    @Resource
    BidService bidService;

    @PostMapping("/selectLimitLike")
    @ApiOperation(value = "分页和模糊查询的接口",notes = "根据分页和用户输入的条件进行查询的方法",response = Map.class,httpMethod = "POST")
    public Map<String,Object> selectLimitLike(@RequestBody @ApiParam(name = "map",required = true,type = "java.util.Map") Map<String,Object> map){

        List<LoanBidsDetail> loanBidsDetailList = loanBidsDetailService.selectLimitLike(map);

        Integer count = loanBidsDetailService.count(map);

        MapUtil mapUtil = new MapUtil();
        if(loanBidsDetailList.size() > 0) {
            mapUtil.put("count",count);
            mapUtil.put("data",loanBidsDetailList);
            mapUtil.setCode(0);
        }else{
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    @PutMapping
    @ApiOperation(value = "修改标的接口",notes = "审核后修改标的的状态和备注",response = Map.class,httpMethod = "PUT")
    public Map<String,Object> update(@RequestBody @ApiParam(name = "loanBidsDetail",required = true,type = "LoanBidsDetail") LoanBidsDetail loanBidsDetail) {

        Integer update = loanBidsDetailService.update(loanBidsDetail);

        MapUtil mapUtil = new MapUtil();
        if(update > 0) {
            mapUtil.setCode(0);
        }else{
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }


    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询全部标的接口",response = Map.class,httpMethod ="GET")
    public Map<String,Object> findAll() {
        List<LoanBidsDetail> loanBidsDetailAll = loanBidsDetailService.findAll();
        MapUtil mapUtil = new MapUtil();
        if (loanBidsDetailAll.size() > 0) {
            mapUtil.put("data", loanBidsDetailAll);
            mapUtil.setCode("0");
            mapUtil.setMsg("标的详情");
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询标的信息接口",response = Map.class,httpMethod ="GET")
    public Map<String,Object> findById(@RequestParam(name = "id",defaultValue = "") Integer id) {
        MapUtil mapUtil = new MapUtil();
        if (loanBidsDetailService.findById(id) != null) {

            mapUtil.setMsg("标的信息");
            mapUtil.put("bid", loanBidsDetailService.findById(id));
            mapUtil.setCode("0");
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加借款申请接口",response = Map.class)
    public Map<String,Object>add(
            @RequestBody @ApiParam (name = ("loanBidsDetail"),value = "申请借款对象",required = true)
                    LoanBidsDetail loanBidsDetail){
        //调用Service添加方法,状态码为0则添加成功,否则抛出异常
        MapUtil mapUtil = new MapUtil();
        if (loanBidsDetailService.add(loanBidsDetail)){
            mapUtil.setMsg("申请成功");
            mapUtil.setCode("0");
            return mapUtil.getMap();
        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
    }

    @GetMapping("/findUserCount")
    @ApiOperation(value = "查询用户放标信息记录",notes = "查询用户借款标信息记录",httpMethod = "Get",response = Map.class)
    public Map<String, Object> findUserCount(Integer userId,Integer page,Integer limit){
        List<Object> byInfo = loanBidsDetailService.selectByInfo(userId, page, limit);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(byInfo);
    }

    @PostMapping("/bid")
    @ApiOperation(value = "用户投标接口",tags = "处理用户投标",httpMethod = "Post",response = MapUtil.class)
    public Map<String, Object> saveBid(@RequestBody @ApiParam Bid bid){
        MapUtil mapUtil = new MapUtil();
        if(bidService.saveBid(bid)){
            mapUtil.setCode(0);
            mapUtil.setMsg("投标成功@@@");
        }else{
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }
    @GetMapping("/findLooseLabel")
    @ResponseBody
    @ApiOperation(value = "首页散标/债权信息",response = List.class,httpMethod = "Get")
    public Map<String,Object>findLooseLabel
            (@RequestParam(name = "id",defaultValue = "") Integer id){
        List<LoanBidsDetail> byLooseLabel = loanBidsDetailService.findLooseLabel(id);
        MapUtil mapUtil=new MapUtil();
        mapUtil.put("data",byLooseLabel);
        mapUtil.setCode("0");

        return mapUtil.getMap();
    }
}